# ./utils/console.py -- Python Console Start
# coding:utf-8


# Import Needs
import inspect
import sys
sys.path.append("..")
from constants import *
from utils.nlp import reply


# Python Console Start Function
def start(loggerset):
    """
    Documentation of function pyrb.main.start
    -----------------------------------------
    Python Console Startpoint
    -----------------------------------------
    start(loggerset)
        Start with console mode, using loggerset for logging.
    """

    loggerset.log("Start Point starting...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    loggerset.log("Setting Variable", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)

    # Set Variable
    mode = "normal"
    
    loggerset.log("Starting Main Loop...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)

    # Main Loop
    while True:
        if mode == "normal":
            prompt = "normal>"
        elif mode == "cal":
            prompt = "normal>"
        elif mode == "cmd":
            prompt = "cmd>"
        elif mode == "python":
            prompt = "python>"
        loggerset.log("Getting input...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        answer = input(prompt)  # Get input
        loggerset.log("Got input: "+answer, level=DEBUG, ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        loggerset.log("Giving output...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        reply_text, reply_mode = reply.get_reply(loggerset, answer, mode)
        print(reply_text)    # Give output
        loggerset.log("Given output.", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
        if reply_mode == "&quit":    # For quit
            loggerset.log("Quiting Main Loop", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
            break
        elif reply_mode == "&cal":  # Calculating mode
            loggerset.log("Starting Calculating Mode...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
            mode = "cal"
        elif reply_mode == "&cmd":  # CMD Execute mode
            loggerset.log("Starting CMD Execute Mode...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
            mode = "cmd"
        elif reply_mode == "&klam":  # Klam Execute mode
            loggerset.log("Starting Klam Execute Mode...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
            mode = "klam"
        elif reply_mode == "&python":  # Python Execute mode
            loggerset.log("Starting Python Execute Mode...", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
            mode = "python"
    loggerset.log("Main Loop ended.", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    loggerset.log("Console Startpoint ended.", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)